﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ConstantCMapEditor3
{
    public partial class AnimateEditor : Form
    {

        static public EditScriptForm editScriptForm = new EditScriptForm();
        public static System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
        public static System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
        public string location = "";

        public AnimateEditor()
        {
            InitializeComponent();
        }

        private void AnimateEditor_Load(object sender, EventArgs e)
        {
                Candidate.Items.Add("CharacterJumpCommands,target,0");
                Candidate.Items.Add("CharacterWalkCommands,target,0,1000,Left");
                Candidate.Items.Add("CharacterEmotionCommands,target,0,Noraml // Normal Happy Amaze Angry");

                Candidate.Items.Add("CharacterSignCommands,target,0,Surprise // Surprise Question");
                Candidate.Items.Add("CharacterCloseSignCommands,target,0");

                Candidate.Items.Add("CameraMoveCommands,0,1000,X,Y");
                Candidate.Items.Add("CameraReleaseCommand,0");
                Candidate.Items.Add("DialogOpenCommand,0");
                Candidate.Items.Add("DialogDisplayCommand,0,500,sentence");
                Candidate.Items.Add("DialogCloseCommand,0");

            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Candidate.SelectedItem != null)
            {
                if (Game1.Selected != null && Game1.Selected.ClassID != null && Candidate.SelectedItem.ToString().Contains("target"))
                {
                    Script.Items.Add(Candidate.SelectedItem.ToString().Replace("target", Game1.Selected.ClassID));
                }
                else
                {
                    Script.Items.Add(Candidate.SelectedItem.ToString());
                }
            }


        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (Script.SelectedItem != null) 
            {
                EditScriptForm.target = Script.SelectedItem.ToString();

                editScriptForm.ShowDialog();

                if (EditScriptForm.ok)
                {
                    int i = Script.SelectedIndex;
                    Script.Items.RemoveAt(i);
                    Script.Items.Insert(i, EditScriptForm.Result);
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Script.SelectedItem != null)
            {
                Script.Items.RemoveAt(Script.SelectedIndex);                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (location.Contains("script"))
            {
                TextWriter tw = new StreamWriter(location, false);

                foreach (Object o in Script.Items)
                {
                    tw.WriteLine(o.ToString());
                }

                tw.Close();
            }
            else 
            {
                button6_Click(sender, e);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            openDialog.Multiselect = false;

            openDialog.Filter = "script files (*.script)|*.script|All files (*.*)|*.*";
            openDialog.FilterIndex = 1;
            openDialog.RestoreDirectory = true;

            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] lines = System.IO.File.ReadAllLines(openDialog.FileName);

                location = openDialog.FileName;

                Script.Items.Clear();

                foreach (String s in lines)
                {
                    Script.Items.Add(s);
                }
            };
        }

        private void button6_Click(object sender, EventArgs e)
        {
            saveDialog.Filter = "script files (*.script)|*.script|All files (*.*)|*.*";
            saveDialog.FilterIndex = 1;
            saveDialog.RestoreDirectory = true;

            if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TextWriter tw = new StreamWriter(saveDialog.FileName, false);

                location = saveDialog.FileName;

                foreach (Object o in Script.Items)
                {
                    tw.WriteLine(o.ToString());
                }

                tw.Close();
            };
        }

        private void Candidate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        

        
    }
}
